package router

import (
	"github.com/gin-gonic/gin"
	"smartposts/internal/handlers"
	"smartposts/internal/middleware"
)

func SetupRouter() *gin.Engine {
	r := gin.Default()

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	r.POST("/login", handlers.Login)
	r.POST("/signup", handlers.CreateUser)

	userRoutes := r.Group("/users")
	userRoutes.Use(middleware.AuthMiddleware())
	{
		userRoutes.GET("/:id", handlers.GetUser)
		userRoutes.PUT("/:id", handlers.UpdateUser)
		userRoutes.DELETE("/:id", handlers.DeleteUser)
	}

	postRoutes := r.Group("/posts")
	postRoutes.GET("/", handlers.GetPosts)
	postRoutes.GET("/:id", handlers.GetPost)
	postRoutes.GET("/hot", handlers.GetHotPostsBasicInfo)
	postRoutes.POST("/:id/ask", handlers.AskPost)
	postRoutes.GET("/:id/ask/stream", handlers.AskPostStream)
	postRoutes.Use(middleware.AuthMiddleware())
	{
		postRoutes.POST("/", handlers.CreatePost)
		postRoutes.PUT("/:id", handlers.UpdatePost)
		postRoutes.DELETE("/:id", handlers.DeletePost)
		postRoutes.POST("/:id/vote", handlers.VotePost)
	}

	voteRoutes := r.Group("/votes")
	voteRoutes.Use(middleware.AuthMiddleware())
	{
		voteRoutes.GET("/", handlers.GetUserVotes)
	}

	return r
}
